package com.feel.mall.userservice.service;

import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.SecureUtil;
import com.feel.mall.common.bean.User;
import com.feel.mall.common.exception.FeelException;
import com.feel.mall.common.service.UserService;
import com.feel.mall.userservice.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Author: 刘永钦
 * @Description:
 */
@DubboService
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public User login(String username, String password) throws FeelException {
        User user = userMapper.selectByUsername(username);
        String passwordMd5 = SecureUtil.md5(SecureUtil.md5(password + user.getSalt()));
        if (!user.getPassword().equals(passwordMd5)) {
            throw new FeelException("密码错误");
        }
        return user;
    }

    @Override
    public Object selectByCondition(User condition, Integer pageNum, Integer pageSize) {
        if (pageNum == null) {
            return userMapper.selectByCondition(condition);
        }
        PageHelper.startPage(pageNum, pageSize);
        List<User> list = userMapper.selectByCondition(condition);
        PageInfo<User> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public void addInfo(User user) throws FeelException {
        User localUser = userMapper.selectByUsername(user.getUsername());
        if (localUser != null) {
            throw new FeelException("账号已经存在。请更换账户");
        }
        String salt = IdUtil.randomUUID().split("-")[0];

        String passwordMd5 = SecureUtil.md5(SecureUtil.md5(user.getPassword() + salt));
        user.setPassword(passwordMd5);
        user.setSalt(salt);
        // user.setStatus(0);
        // user.setMoney(new BigDecimal("0"));
        // user.setRegTime(new Date());

    //     插入数据库
        userMapper.addInfo(user);
    }

    @Override
    public User selectById(Integer id) {
        User user = userMapper.selectById(id);
        return user;
    }

    @Override
    public void updateInfo(User user) {
        // todo 对部分数据进行判断
        userMapper.updateInfo(user);
    }

    @Override
    public Boolean changePwd(String password, String newPassword, Integer userId) throws FeelException {
        User user = userMapper.selectById(userId);
        String salt = user.getSalt();

        String passwordMd5 = SecureUtil.md5(SecureUtil.md5(password + salt));
        if (!user.getPassword().equals(passwordMd5)) {
            throw new FeelException("密码错误请重试");
        }

        String newPasswordMd5 = SecureUtil.md5(SecureUtil.md5(newPassword + salt));

        user.setPassword(newPasswordMd5);

        userMapper.updateInfo(user);
        return true;
    }

    @Override
    public Boolean verifyPayPassword(String payPassword, Integer userId) throws FeelException {
        User user = userMapper.selectById(userId);
        if (user.getPayPassword() == null) {
            return true;
        }

        String passwordMd5 = SecureUtil.md5(SecureUtil.md5(payPassword + user.getSalt()));
        if (!user.getPayPassword().equals(passwordMd5)) {
            throw new FeelException("密码错误");
        }
        return true;
    }

    @Override
    public Boolean changePayPassword(String payPassword, String newPayPassword, Integer userId) throws FeelException {
        User user = userMapper.selectById(userId);
        // 密码加密处理
        String payPasswordMd5 = SecureUtil.md5(SecureUtil.md5(payPassword + user.getSalt()));

        if (user.getPayPassword() != null && !user.getPayPassword().equals(payPasswordMd5)) {
            throw new FeelException("密码错误");
        }

        String newPayPasswordMd5 = SecureUtil.md5(SecureUtil.md5(newPayPassword + user.getSalt()));

        user.setPayPassword(newPayPasswordMd5);

        userMapper.updateInfo(user);
        return true;
    }

    @Override
    public Boolean rechargeBalance(BigDecimal money, Integer userId) throws FeelException {
        User user = userMapper.selectById(userId);

        //  判断用户状态
        if (user.getStatus().equals(0)) {
            throw new FeelException("请完成认证后再充值");
        }
        if (user.getStatus() >= 2) {
            throw new FeelException("请检查账户状态再进行充值");
        }
        BigDecimal newMoney = user.getMoney().add(money);

        user.setMoney(newMoney);

        userMapper.updateInfo(user);
        return true;
    }
}
